/**
 *	插件:短信使用
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-19
 */


package com.framework.Chajian;


import com.framerock.db.DBConn;
import com.framerock.server.Response;
import com.framerock.baselib.CDate;
import com.framerock.baselib.Jiami;
import com.framerock.baselib.Rock;
import com.framerock.baselib.RockMsg;
import com.framerock.baselib.Xinhu;
import com.framework.ACore.Chajian;
import com.framework.Model.ModelOption;

import java.util.Map;
import java.util.Random;

public class ChajianSms extends Chajian {



    public ChajianSms(DBConn dbs) {
        super(dbs);
    }


    public static ChajianSms obj(DBConn dbs, String type)
    {
        ChajianSms obj = new ChajianSms(dbs);
        if(type.equals("yunpian")) obj = new ChajianSmsYunpian(dbs);
        if(type.equals("xinhu") || Rock.isEmpt(type)) obj = new ChajianSmsXinhu(dbs);
        return obj;
    }

    public static ChajianSms obj(DBConn dbs)
    {
        String smstype = Xinhu.getVal(dbs, "smstype");
        return obj(dbs, smstype);
    }

    /**
     * 发送验证码
     * */
    public String sendCode(String mobile, String key)
    {
        return Response.returnError("not dev");
    }

    /**
     * 获取验证码
     * */
    protected RockMsg getCode(String mobile, String key)
    {
        RockMsg rockmsg = new RockMsg();
        String okey     = Jiami.md5(""+mobile+""+key+"");
        ModelOption obj = ModelOption.get(this.nDb);
        String optdt    = obj.getVal(okey,"", "optdt");
        if(!Rock.isEmpt(optdt)) {
            String time1 = CDate.now("time", optdt);
            String time2 = CDate.gettime();
            Long t1 = Long.parseLong(time1);
            Long t2 = Long.parseLong(time2);
            Long jg = t2 - t1;
            if(jg < 1 * 60 *1000)return rockmsg.error("获取太快了");
        }
        Random random   = new Random();
        int randomNumber= random.nextInt(900000) + 100000;
        String code     = ""+randomNumber+"";
        obj.setVal(okey, Jiami.md5(""+mobile+""+code+""));
        return rockmsg.success(code);
    }

    /**
     * 验证验证码是不是对的
     * */
    public RockMsg CheckCode(String mobile, String key, String yzm)
    {
        RockMsg rockmsg = new RockMsg();
        String okey     = Jiami.md5(""+mobile+""+key+"");
        ModelOption obj = ModelOption.get(this.nDb);
        Map<String, String> rs = obj.getOne("`num`='"+okey+"'");
        if(rs==null)return rockmsg.error("请先获取验证码");
        String optdt    = rs.get("optdt");
        String time1 = CDate.now("time", optdt);
        String time2 = CDate.gettime();
        Long t1 = Long.parseLong(time1);
        Long t2 = Long.parseLong(time2);
        Long jg = t2 - t1;
        if(jg > 5 * 60 *1000)return rockmsg.error("无效验证码");
        String val      = Jiami.md5(""+mobile+""+yzm+"");
        if(rs.get("value").equals(val)){
            obj.delete(rs.get("id"));
            return rockmsg.success("ok");
        }
        return rockmsg.error("验证码错误");
    }

    /**
     * 发送短信
     * */
    public String send(String mobile, String qianm, String tplnum, Map<String, String> params)
    {
        return Response.returnError("not dev");
    }
}
